/**
 * 
 */
package fr.ants.view.component;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import fr.ants.controller.clock.ClockState;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.view.Items;
import fr.ants.view.PictureManage;

/**
 * This panel contain all control panel on the simulation.
 * 
 * @author Nyu
 */
public class ControlButtonsPanel extends JPanel implements
		PropertyChangeListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7507683188427180311L;

	/**
	 * button to stop the simulation
	 */
	private final JButton stopButton;

	/**
	 * button to play the simulation
	 */
	private final JButton playPauseButton;

	/**
	 * button to accelerate the simulation
	 */
	private final JButton accelerateButton;

	/**
	 * button to slow the simulation
	 */
	private final JButton slowButton;

	/**
	 * Build.
	 * @param buttonsListener listener for the buttons
	 */
	public ControlButtonsPanel(final ButtonsListener buttonsListener) {
		// slow button
		slowButton = new JButton(PictureManage.createIconToolBar(Items.SLOW
				.getIcon()));
		slowButton.setToolTipText(Items.SLOW.getLabel());
		slowButton.setActionCommand(Items.SLOW.getActionCommand());
		slowButton.addMouseListener(buttonsListener);
		slowButton.setEnabled(false);
		this.add(slowButton);

		// stop button
		stopButton = new JButton(PictureManage.createIconToolBar(Items.STOP
				.getIcon()));
		stopButton.setToolTipText(Items.STOP.getLabel());
		stopButton.setActionCommand(Items.STOP.getActionCommand());
		stopButton.addMouseListener(buttonsListener);
		stopButton.setEnabled(false);
		this.add(stopButton);

		// play and pause button
		playPauseButton = new JButton(
				PictureManage.createIconToolBar(Items.PLAY.getIcon()));
		playPauseButton.setToolTipText(Items.PLAY.getLabel());
		playPauseButton.setActionCommand(Items.PLAY.getActionCommand());
		playPauseButton.addMouseListener(buttonsListener);
		playPauseButton.setEnabled(false);
		this.add(playPauseButton);

		// accelerate buttons
		accelerateButton = new JButton(
				PictureManage.createIconToolBar(Items.ACCELERATE.getIcon()));
		accelerateButton.setToolTipText(Items.ACCELERATE.getLabel());
		accelerateButton.setActionCommand(Items.ACCELERATE.getActionCommand());
		accelerateButton.addMouseListener(buttonsListener);
		accelerateButton.setEnabled(false);
		this.add(accelerateButton);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		if ("grid".equals(evt.getPropertyName())) {
			playPauseButton.setEnabled(true);
		} else if ("state".equals(evt.getPropertyName())) {
			if (evt.getNewValue() instanceof ClockState) {
				ClockState state = (ClockState) evt.getNewValue();
				switch (state) {
				case PAUSED:
					playPauseButton.setIcon(PictureManage
							.createIconToolBar(Items.PLAY.getIcon()));
					playPauseButton.setToolTipText(Items.PLAY.getLabel());
					accelerateButton.setEnabled(false);
					slowButton.setEnabled(false);
					stopButton.setEnabled(true);
					break;
				case RUNNING:
					playPauseButton.setIcon(PictureManage
							.createIconToolBar(Items.PAUSE.getIcon()));
					playPauseButton.setToolTipText(Items.PAUSE.getLabel());
					accelerateButton.setEnabled(true);
					slowButton.setEnabled(true);
					stopButton.setEnabled(true);
					break;
				case STOPPED:
					playPauseButton.setIcon(PictureManage
							.createIconToolBar(Items.PLAY.getIcon()));
					playPauseButton.setToolTipText(Items.PLAY.getLabel());
					accelerateButton.setEnabled(false);
					slowButton.setEnabled(false);
					stopButton.setEnabled(false);
					break;
				default:
					break;
				}
			}
		}
	}
}